<template>
  <div class="article-page">
    <div class="banner">
      <div class="container">
        <h1>{{ title }}</h1>

        <article-meta
          :slug="slug"
          :author="author"
          :created-at="createdAt"
          :favorited="favorited"
          :favorites-count="favoritesCount"
          :favorite-disabled="favoriteLoading"
          :follow-disabled="followLoading"
          @follow="handleFollow"
          @favorite="handleFavorite"
          @delete="handleDelete"
        />
      </div>
    </div>

    <div class="container page">
      <div class="row article-content">
        <div class="col-md-12" v-html="body"></div>
      </div>

      <hr />

      <div class="article-actions">
        <article-meta
          :slug="slug"
          :author="author"
          :created-at="createdAt"
          :favorited="favorited"
          :favorites-count="favoritesCount"
          :favorite-disabled="favoriteLoading"
          :follow-disabled="followLoading"
          @follow="handleFollow"
          @favorite="handleFavorite"
          @delete="handleDelete"
        />
      </div>

      <div class="row">
        <div class="col-xs-12 col-md-8 offset-md-2">
          <ArticleComment :slug="slug" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getArticle,
  favoriteArticle,
  unfavoriteArticle,
  deleteArticle,
} from '@/api/article'
import { follow, unfollow } from '@/api/profile'
import MDI from 'markdown-it'
import ArticleMeta from '@/pages/article/components/meta'
import ArticleComment from '@/pages/article/components/comment'

export default {
  name: 'ArticleIndex',
  components: { ArticleMeta, ArticleComment },
  async asyncData({ params }) {
    const { slug } = params
    const { article } = await getArticle(slug)
    article.body = new MDI().render(article.body)
    return {
      ...article,
    }
  },
  data() {
    return {
      followLoading: false,
      favoriteLoading: false,
    }
  },
  methods: {
    handleFollow() {
      this.followLoading = true
      const fo = this.author.following ? unfollow : follow
      fo(this.author.username)
        .then(() => {
          this.author.following = !this.author.following
        })
        .finally(() => {
          this.followLoading = false
        })
    },
    handleFavorite() {
      this.favoriteLoading = true
      const fo = this.favorited ? unfavoriteArticle : favoriteArticle
      fo(this.author.username)
        .then(() => {
          this.favoritesCount += this.favorited ? -1 : 1
          this.favorited = !this.favorited
        })
        .finally(() => {
          this.favoriteLoading = false
        })
    },
    handleDelete() {
      deleteArticle(this.slug).then(() => {
        this.$router.push({ name: 'home' })
      })
    },
  },
  head() {
    return {
      title: `${this.title} - RealWorld`,
      meta: [
        {
          hid: 'description',
          name: 'description',
          content: this.description,
        },
      ],
    }
  },
}
</script>

<style></style>
